package com.lyk.coursearrange.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyk.coursearrange.common.ServerResponse;
import com.lyk.coursearrange.dao.CoursePlanDao;
import com.lyk.coursearrange.entity.*;
import com.lyk.coursearrange.entity.response.CoursePlanVo;
import com.lyk.coursearrange.service.CourseInfoService;
import com.lyk.coursearrange.service.CoursePlanService;
import com.lyk.coursearrange.service.CoursePlanWithTeacherCourseService;
import com.lyk.coursearrange.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * @author xqj
 * @since 2023-03-21
 */
@RestController
public class CoursePlanController {

    @Autowired
    private CourseInfoService courseInfoService;
    @Autowired
    private CoursePlanService coursePlanService;

    @Autowired
    private TeacherService teacherService;

    /**
     * 根据班级查询课程表
     * @param classNo
     * @return
     */
    @GetMapping("/courseplan/{classno}/{semester}")
    public ServerResponse queryCoursePlanByClassNo(@PathVariable("classno") String classNo,
                                                   @PathVariable("semester") String semester) {
        QueryWrapper<CoursePlan> wrapper = new QueryWrapper<CoursePlan>().eq("class_no", classNo).eq("semester", semester).orderByAsc("class_time");
        List<CoursePlan> coursePlanList = coursePlanService.list(wrapper);
        List<CoursePlanVo> coursePlanVos = new LinkedList<>();
        coursePlanList.forEach(v->{
            CoursePlanVo coursePlanVo = JSON.parseObject(JSON.toJSONString(v), CoursePlanVo.class);
            coursePlanVo.setCourseInfo(courseInfoService.getOne(new QueryWrapper<CourseInfo>().eq("course_no",v.getCourseNo())));
            coursePlanVo.setTeacher(teacherService.getOne(new QueryWrapper<Teacher>().eq("teacher_no",v.getTeacherNo())));
            coursePlanVos.add(coursePlanVo);
        });

        return ServerResponse.ofSuccess(coursePlanVos);
    }
    /**
     * 根据班级查询课程表
     * @param classNo
     * @return
     */
    @GetMapping("/courseplan/{classno}")
    public ServerResponse queryCoursePlanByStudent(@PathVariable("classno") String classNo
                                                   ) {
        String semester ="2022-2023-1";
        QueryWrapper<CoursePlan> wrapper = new QueryWrapper<CoursePlan>().eq("class_no", classNo).eq("semester", semester).orderByAsc("class_time");
        List<CoursePlan> coursePlanList = coursePlanService.list(wrapper);
        List<CoursePlanVo> coursePlanVos = new LinkedList<>();
        coursePlanList.forEach(v->{
            CoursePlanVo coursePlanVo = JSON.parseObject(JSON.toJSONString(v), CoursePlanVo.class);
            coursePlanVo.setCourseInfo(courseInfoService.getOne(new QueryWrapper<CourseInfo>().eq("course_no",v.getCourseNo())));
            coursePlanVo.setTeacher(teacherService.getOne(new QueryWrapper<Teacher>().eq("teacher_no",v.getTeacherNo())));
            coursePlanVos.add(coursePlanVo);
        });

        return ServerResponse.ofSuccess(coursePlanVos);
    }
    /**
     * 根据班级查询课程表
     * @param teacherNo
     * @return
     */
    @GetMapping("/courseplanbyteacher/{teacherNo}/{semester}")
    public ServerResponse queryCoursePlanByTeacherNo(@PathVariable("teacherNo") String teacherNo,
                                                   @PathVariable("semester") String semester) {
        QueryWrapper<CoursePlan> wrapper = new QueryWrapper<CoursePlan>().eq("teacher_no", teacherNo).eq("semester", semester).orderByAsc("class_time");
        List<CoursePlan> coursePlanList = coursePlanService.list(wrapper);
        List<CoursePlanVo> coursePlanVos = new LinkedList<>();
        coursePlanList.forEach(v->{
            CoursePlanVo coursePlanVo = JSON.parseObject(JSON.toJSONString(v), CoursePlanVo.class);
            coursePlanVo.setCourseInfo(courseInfoService.getOne(new QueryWrapper<CourseInfo>().eq("course_no",v.getCourseNo())));
            coursePlanVo.setTeacher(teacherService.getOne(new QueryWrapper<Teacher>().eq("teacher_no",v.getTeacherNo())));
            coursePlanVos.add(coursePlanVo);
        });

        return ServerResponse.ofSuccess(coursePlanVos);
    }
    /**
     * 查询所有课程
     */
    @GetMapping("/classtask/{page}")
    public ServerResponse getAllCourse(@PathVariable("page") Integer page,
                                         @RequestParam(defaultValue = "10") Integer limit) {
        Page<CoursePlan> pages = new Page<>(page, limit);
        QueryWrapper<CoursePlan> wrapper = new QueryWrapper<CoursePlan>();
        wrapper.orderByAsc("id");
        IPage<CoursePlan> ipage = coursePlanService.page(pages, wrapper);

        if (ipage != null) {
            return ServerResponse.ofSuccess(ipage);
        }
        return ServerResponse.ofError("查询课程失败！");
    }
    @GetMapping("/classtask/test")
    public ServerResponse getTest(
                                       ) {
        coursePlanDao.deleteCoursePlanBySemester("2022-2023-2");

            return ServerResponse.ofSuccess();

    }
    /**
     * 更新排课信息
     * @param id
     * @param courseInfo
     * @return
     */
    @PostMapping("/modify/{id}/")
    public ServerResponse modifyCourseInfo(@PathVariable("id") Integer id, @RequestBody CoursePlan courseInfo) {
        QueryWrapper<CoursePlan> wrapper = new QueryWrapper<CoursePlan>().eq("id", id);
        boolean b = coursePlanService.update(courseInfo, wrapper);
        if (b) {
            return ServerResponse.ofSuccess("更新成功");
        }
        return ServerResponse.ofError("更新失败");
    }
/**
 *  不带检测修改课表
 */

    @Autowired
    CoursePlanDao coursePlanDao;
    @PostMapping("/updateCoursePlan")
    public String updateCoursePlan(@RequestBody CoursePlan coursePlan) {
        String message1 = null;
        try {
            //coursePlanDao.updateCoursePlanWithNoCheck(coursePlan.getId(), coursePlan.getClassroomNo(), coursePlan.getClassTime());
            CoursePlan coursePlan1= coursePlanDao.selectById(coursePlan.getId());
            coursePlan1.setClassroomNo(coursePlan.getClassroomNo());
            coursePlan1.setClassTime(coursePlan.getClassTime());
            coursePlanDao.updateById(coursePlan1);
            return "success";
        } catch (DuplicateKeyException e) {
            String message = String.valueOf(coursePlanDao.findDuplicateCoursePlan(coursePlan.getId(), coursePlan.getClassroomNo(), coursePlan.getClassTime()));
            if (message != null) {
                return message;
            } else {
                throw e;
            }
        } catch (Exception e) {
            throw e;
        }
    }
    /**
     * 查询老师课表
     */
    @GetMapping("/classPlan/teacher/{teacherNo}/{page}")
    public ServerResponse getAllCourseByTeacher(@PathVariable("page") Integer page,
                                                @PathVariable("teacherNo") String teacherNo,
                                       @RequestParam(defaultValue = "30") Integer limit) {
        Page<CoursePlan> pages = new Page<>(page, limit);
        QueryWrapper<CoursePlan> wrapper = new QueryWrapper<CoursePlan>();
        wrapper.eq("teacher_no",teacherNo);
        IPage<CoursePlan> ipage = coursePlanService.page(pages, wrapper);

        if (ipage != null) {
            return ServerResponse.ofSuccess(ipage);
        }
        return ServerResponse.ofError("查询教师课程失败！");
    }
    /**
     * 查询老师课表
     */
    @GetMapping("/classPlan/teacher1/{semester}/{teacherNo}")
    public ServerResponse getAllCourseByTeacherWithSemester(@PathVariable("semester") String semester,
                                                @PathVariable("teacherNo") String teacherNo,
                                                @RequestParam(defaultValue = "30") Integer limit) {
        QueryWrapper<CoursePlan> wrapper = new QueryWrapper<CoursePlan>();
        wrapper.eq("teacher_no",teacherNo).eq("semester",semester);
        List<CoursePlan> ipage = coursePlanService.list(wrapper);

        if (ipage != null) {
            return ServerResponse.ofSuccess(ipage);
        }
        return ServerResponse.ofError("查询教师课程失败！");
    }
    /**
     * 查询班级课表
     */
    @GetMapping("/classPlan/class/{classNo}")
    public ServerResponse getAllCourseByTeacher(
                                                @PathVariable("classNo") String classNo,
                                                @RequestParam(defaultValue = "10") Integer limit) {

        QueryWrapper<CoursePlan> wrapper = new QueryWrapper<CoursePlan>();
        wrapper.eq("class_no",classNo).orderByAsc("class_time");
        List<CoursePlan> list =coursePlanService.list(wrapper);

        if (list != null) {
            return ServerResponse.ofSuccess(list);
        }
        return ServerResponse.ofError("查询课程失败！");
    }
    /**
     * 查询班级的学期课表
     */
    @GetMapping("/classPlan/class/{classNo}/{semester}")
    public ServerResponse getAllCourseByTeacher(
            @PathVariable("classNo") String classNo,
            @PathVariable("semester") String semester,
            @RequestParam(defaultValue = "10") Integer limit) {

        QueryWrapper<CoursePlan> wrapper = new QueryWrapper<CoursePlan>();
        wrapper.eq("class_no",classNo).eq("semester",semester).orderByAsc("class_time");
        List<CoursePlan> list =coursePlanService.list(wrapper);

        if (list != null) {
            return ServerResponse.ofSuccess(list);
        }
        return ServerResponse.ofError("查询课程失败！");
    }
    /**
     * 查询挂起课表
     */
    @GetMapping("/classPlan/isDeleted/{page}")
    public ServerResponse getAllCourseIsDeleted(@PathVariable("page") Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit) {
        Page<CoursePlan> pages = new Page<>(page, limit);
        QueryWrapper<CoursePlan> wrapper = new QueryWrapper<CoursePlan>();
        short i = 0;
        wrapper.eq("weeks_sum",i);
        IPage<CoursePlan> ipage = coursePlanService.page(pages, wrapper);

        if (ipage != null) {
            return ServerResponse.ofSuccess(ipage);
        }
        return ServerResponse.ofError("查询课程失败！");
    }
}

